import pandas as pd
import numpy as np

import matplotlib.pyplot as plt

def filter(x=[0]*20,r=1e-3,q=1e-5):  # 这个函数需要重复调用，所以封起来。x是一个1行20列的数。
    a = 0
    k = [0] * 20
    m = [0] * 20
    p = [0] * 20
    y = [0] * 20
    size = len(x)
    m[0]=x[0]

    while a < size-1:
        k[a + 1] = p[a] / (p[a] + r)
        m[a + 1] = m[a] + k[a + 1] * (x[a+1] - m[a])
        p[a + 1] = p[a] - k[a + 1] * p[a] + q

        a = a + 1
    return m

def filterUtils(data=0,filterQueue=[],r=0,q=0):
    result = 0
    if (len(filterQueue) == 0):
        filterQueue = [data] * 20
        result = data
    else:
        filterQueue.pop(0)
        filterQueue.append(data)
        result = filter(filterQueue, r=r, q=q)[-1]  # 取倒数第一项
    return result,filterQueue






